import apis from './config';
import { http } from '../utils/http';

const apiModule = {};
Object.keys(apis).forEach(item => {
  apiModule[item] = options => {
    return new Promise(async (resolve, reject) => {
      try {
        if (apis[item].method === 'POST') {
          const { data: res } = await http.post(apis[item].url, options);
          resolve(res);
        } else {
          const { data: res } = await http.get(apis[item].url, { params: options });
          resolve(res);
        }
      } catch (e) {
        reject(e);
      }
    });
  };
});

function getApi(apiName, options) {
  if (!apiModule[apiName]) {
    throw new Error(`api:[${apiName}]未找到`);
  }
  return apiModule[apiName](options);
}

export default getApi;
